package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.PutRecordAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.ReasonAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.RepairRecordAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.keepRecordAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.Device;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.ReasonBen;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.preference.PreferencesUtil;
import com.nantai.jiafang.nantaijiafang.deviceManager.utils.MessagePop;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

public class repairActivity extends AppCompatActivity {

    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
    @InjectView(R.id.image_toolbar)
    TextView mImageToolbar;
    @InjectView(R.id.toolbar_menu)
    TextView mToolbarMenu;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.layout_tool_bar)
    LinearLayout mLayoutToolBar;
    @InjectView(R.id.name_tv)
    TextView mNameTv;
    @InjectView(R.id.qrcode_tv)
    TextView mQrcodeTv;
    @InjectView(R.id.status_tv)
    TextView mStatusTv;
    @InjectView(R.id.place_tv)
    TextView mPlaceTv;
    @InjectView(R.id.send_btn)
    Button mSendBtn;
    @InjectView(R.id.repairlist)
    ListView mRepairlist;

    @InjectView(R.id.repair_reason_et)
    TextView mRepairReasonEt;
    @InjectView(R.id.repair_back)
    LinearLayout mRepairBack;

    Device mDevice = null;
    MaterialDialog showDialog;
    String reasonId=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair);
        ButterKnife.inject(this);
        mTvToolbarTitle.setText("维修");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        Intent intent = this.getIntent();
        mDevice = (Device) intent.getSerializableExtra("deviceInfo");

        showDialog = new MaterialDialog.Builder(repairActivity.this)
                .content("正在请求数据")
                .progress(true, 0, false)
                .cancelable(false).build();
        initView();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            setIntent(intent);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        Intent intent =getIntent();
        String reasonid= intent.getStringExtra("reasonId");
        String content = intent.getStringExtra("content");
        if(content!=null)
        {
            mRepairReasonEt.setText(content);
        }
        if(reasonid!=null)
        {
            reasonId=reasonid;
        }
    }

    private void initView() {
        mNameTv.setText("设备名称:  " + mDevice.getInfo().getDevice_name());
        mQrcodeTv.setText("设备条码:  " + mDevice.getInfo().getDevice_qrcode());
        mStatusTv.setText("设备状态:  " + mDevice.getInfo().getStatus());
        String place = mDevice.getInfo().getPlace_name();
        mPlaceTv.setText("设备位置:  " + (place == null ? "未知" : place));

        if(mDevice.getInfo().getStatus().equals("维修"))
        {
            mSendBtn.setText("结束维修");
            mRepairBack.setVisibility(View.VISIBLE);

        }else
        {
            mSendBtn.setText("维修");
            mRepairBack.setVisibility(View.GONE);
        }
        List repairList = mDevice.getRepaire_record();
        if (repairList == null)
            repairList = new ArrayList();
        mRepairlist.setAdapter(new RepairRecordAdapter(repairList, this));
        mRepairlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    @OnClick({R.id.send_btn,R.id.repairSelect})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.repairSelect:{
                Intent intent = new Intent(repairActivity.this, ReasonActivity.class);
                intent.putExtra("reasonType", "0");
                String deviceType= mDevice.getInfo().getDevice_qrcode().substring(0,3);
                intent.putExtra("deviceType",deviceType);
                startActivity(intent);
            }break;
            case R.id.send_btn:{
                if(mDevice.getInfo().getStatus().equals("维修"))
                {
                    if(reasonId==null)
                    {
                        MessagePop.ToastMessage(repairActivity.this, "请选择维修原因");
                        return;
                    }
                    requestRepairDevice(mDevice.getInfo().getDevice_qrcode(),"1",reasonId);
                }else
                {
                    requestRepairDevice(mDevice.getInfo().getDevice_qrcode(),"0","");
                }
            }break;
        }

    }
    private void requestRepairDevice(String qrcode, String operate,String reason) {
        if (qrcode.length() != 12) {
            MessagePop.ToastMessage(repairActivity.this, "请扫描正确的条码");
            return;
        }
        showDialog.show();
        final  String operateStr =operate;
        HttpClient client = new HttpClient(repairActivity.this);
        Call call = client.service().repaire(qrcode, operateStr,reason);
        client.request(call, new ResponseHandler<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody repairStatus) {
                //登陆成功，保存用户信息
                showDialog.dismiss();
                if (operateStr.equals("0")) {
                    MessagePop.ToastMessage(repairActivity.this, "开始维修");
                    finish();
                }
                else {
                    MessagePop.ToastMessage(repairActivity.this, "维修完成");
                    finish();
                }
            }

            @Override
            public void onFailure(int code, Error e) {
                showDialog.dismiss();
                if (code != 404) {
                    MessagePop.ToastMessage(repairActivity.this, e.getMessage());
                } else {
                    MessagePop.ToastMessage(repairActivity.this, "网络异常");
                }

            }
        });
    }
}
